package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.MessageModel;
import com.jp.knowledge.util.ImageLoadUtil;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/5/10.
 */

public class MessageAdapter extends RecycleVAdapter<MessageModel> {

    private SimpleDateFormat dateFormat;

    public MessageAdapter(Context context, @NonNull List<MessageModel> list) {
        super(context, list);
        this.dateFormat = new SimpleDateFormat("HH:mm");
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_message;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        MessageModel data = getData(position);
        ImageView messageIcon = holder.getImageView(R.id.message_icon);
        ImageLoadUtil.load(mContext, data.getIcon(), messageIcon);
        messageIcon.setBackground(getBg(data.getColor()));
        if(data.getCount() == 0){
            holder.get(R.id.message_count).setVisibility(View.GONE);
        } else{
            holder.get(R.id.message_count).setVisibility(View.VISIBLE);
            holder.setTextView(R.id.message_count, data.getCount() + "");
        }
        holder.setTextView(R.id.message_title, data.getName());
        holder.setTextView(R.id.message_desc, data.getMessage());
        holder.setTextView(R.id.message_time, dateFormat.format(data.getTime()));
    }

    private GradientDrawable getBg(String color){
        try {
            GradientDrawable gd = new GradientDrawable();
            gd.setColor(Color.parseColor(color));
            gd.setCornerRadius(10);
            gd.setStroke(1, Color.parseColor(color));
            return gd;
        } catch (Exception e){
            return null;
        }
    }
}
